package normalsys.demo.controller;

import normalsys.demo.base.config.ResponseCode;
import normalsys.demo.base.exp.BizException;
import normalsys.demo.base.page.Page;
import normalsys.demo.base.util.string.StringUtil;
import normalsys.demo.bean.CommonQueryParamBean;
import normalsys.demo.bean.UserParamBean;
import normalsys.demo.entity.User;
import normalsys.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Project Name: ms
 * File Name: UserController
 * Package Name: ms.pms.controller
 * Date: 2018/6/21  15:43
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param commonQueryParamBean
     * @return
     */
    @PostMapping("/query")
    public Page<User> query(@RequestBody CommonQueryParamBean commonQueryParamBean) {
        if(StringUtil.isEmpty(commonQueryParamBean.getPageNo())) {
            commonQueryParamBean.setPageNo(1);
        }
        if(StringUtil.isEmpty(commonQueryParamBean.getPageSize())) {
            commonQueryParamBean.setPageSize(10);
        }
        if(StringUtil.isEmpty(commonQueryParamBean.getStatus())) {
            commonQueryParamBean.setStatus(0);
        }

        Page<User> page = userService.query(commonQueryParamBean);
        return page;
    }

    /**
     * 保存
     * @param userParamBean
     * @return
     */
    @PostMapping("/save")
    public void save(@RequestBody UserParamBean userParamBean) {
        //参数校验
        checkParam(userParamBean);

        userService.save(userParamBean);
    }



    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/use/{id}")
    public void use(@PathVariable("id") Long id) {
        userService.use(id);
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable("id") Long id) {
        userService.cancel(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
        userService.delete(id);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    public User detail(@PathVariable("id") Long id) {
        return userService.detail(id);
    }



    /**
     * 校验新增和修改参数
     * @param userParamBean
     */
    private void checkParam(UserParamBean userParamBean) {
        if(StringUtil.isEmpty(userParamBean.getId())) {
            userParamBean.setImg("");
            if(StringUtil.isEmpty(userParamBean.getAccount())) {
                throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户账号不能为空");
            }
            if(StringUtil.isEmpty(userParamBean.getPwd())) {
                throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户密码不能为空");
            }
        }
        if(StringUtil.isEmpty(userParamBean.getName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户名称不能为空");
        }
        if(StringUtil.isEmpty(userParamBean.getTel())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户电话不能为空");
        }
        if(StringUtil.isEmpty(userParamBean.getStatus())) {
            userParamBean.setStatus(2);
        }

    }

}
